package com.cke.yang.common.infra.utils;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.Objects;

/**
 * 分页工具类
 *
 * @author yuyang
 * @date 2025-11-22 00:00:00
 */
public class PageUtils {

    public static  <T> PageInfo<T> doPage(Pageable page, ISelect select) {
        // 从 Pageable 提取排序参数
        if (page.getSort().isSorted()) {
            Sort.Order order = page.getSort().get().findFirst().orElse(null);
            if (Objects.nonNull(order)) {
                String orderBy = order.getProperty() + " " + order.getDirection().name().toLowerCase();
                return PageHelper
                        .startPage(page.getPageNumber(), page.getPageSize(), orderBy)
                        .doSelectPageInfo(select);
            }
        }
        return PageHelper
                .startPage(page.getPageNumber(), page.getPageSize())
                .doSelectPageInfo(select);
    }

}
